package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.TeamUserDao;
import com.hhit.collaborative_learning.dao.UserDao;
import com.hhit.collaborative_learning.dao.impl.TeamUserDaoImpl;
import com.hhit.collaborative_learning.dao.impl.UserDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.DateUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.hhit.collaborative_learning.utils.StringUtil;
import com.mysql.jdbc.StringUtils;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class UpdateCoopUserServlet
 */
@WebServlet("/communication/UpdateCoopUserServlet")
public class UpdateCoopUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateCoopUserServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String teamID = request.getParameter("teamID");
		String[] userDesc = request.getParameterValues("userDesc[]");
		String[] userName = request.getParameterValues("userName[]");
		String uname = StringUtil.formatArraytoString(0, userName, ",");
		System.out.println("uname  "+uname);
		List<Integer> uId = new ArrayList<Integer>();
		Map<String, Integer> uid_name=new HashMap<String, Integer>();
		Map<String, String> udesc_name=DateUtil.getMapFromStringArray(userName, userDesc);
		System.out.println("udesc_name size "+udesc_name.size()+" userName"+userName.length +"  userDesc"+userDesc.length);
		Map<Integer,String> resultMap= new HashMap<Integer, String>();
		JSONObject jobj = new JSONObject();
		String isSuccess = "false";
		UserDao ud = new UserDaoImpl();
		TeamUserDao tud = new TeamUserDaoImpl();
		Connection con = DBAccess.getInstance().getConn();
		
		if (!StringUtils.isNullOrEmpty(uname)) {
			uname = " u.userNickName in (" + uname + ")";
		}
		ResultSet reUid = ud.queryUserId(con, 0, 0, uname, null, null);

		try {
			while (reUid.next()) {
				uid_name.put(reUid.getString(2), reUid.getInt(1));
			}
			System.out.println("uid_name "+uid_name.size());
			if (uid_name.size() != udesc_name.size()) {
				System.out.println("添加的某些用户不存在");
			} else {
				resultMap=DateUtil.getMapFromMap(uid_name, udesc_name);
				System.out.println("resultMap "+resultMap.size());
				if (!StringUtils.isNullOrEmpty(teamID)) {
					int rte = tud.deletArray(con, " teamId=" + teamID);
					int rtu = tud.insertArray(con, resultMap, "" + teamID);
					System.out.println("删除"+rte+"添加"+rtu);
					System.out.println("rtu 添加  " +rtu);
					isSuccess = "true";
				}
			}
			jobj.put("isSuccess", isSuccess);
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(con, null, null);
			System.out.println("CoopUser json " + jobj.toString());
		}
	}

}
